<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Model\readuser;
use App\Http\Requests;

class qqController extends Controller
{
      public function qq(Request $request)
    {

           
            // Step2：获取Authorization Code
            header("Content-type: text/html; charset=utf-8");
            $code = $_GET['code'];
            // $code=$request->code;

// Step3：通过Authorization Code获取Access Token
            $url = 'https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&client_id=101392477&client_secret=db27885be67e5ea942e9e9eb09701cb0&code=' . $code . '&redirect_uri=http://www.tzvk.vip/login';
             $res = $this->https_request($url);

           
            $res = explode('&', $res);
            $res = explode('=', $res[0]);

            $access_token = $res[1];


// Step4：使用Access Token来获取用户的OpenID

            $openid_url = 'https://graph.qq.com/oauth2.0/me?access_token=' . $access_token;
// var_dump($openid_url);
// exit;
            $openid = $this->https_request($openid_url);


            if (strpos($openid, "callback") !== false) {
                $lpos = strpos($openid, "(");
                $rpos = strrpos($openid, ")");
                $openid = substr($openid, $lpos + 1, $rpos - $lpos - 1);
            }
            $user = json_decode($openid, true);
// var_dump($user);
// exit;
            $openid_info = $user['openid'];

            $userinfo_url = 'https://graph.qq.com/user/get_user_info?access_token=' . $access_token . '&oauth_consumer_key=101392477&openid=' . $openid_info;

            $userinfo =  $this->https_request($userinfo_url);
            $userinfo=json_decode($userinfo);
            $a=array();
            $a=get_object_vars($userinfo);
            $readname=$a['nickname'];
            $new=readuser::where('nickname',$readname)->get();
            if (!empty(count($new))) {
                 foreach ($new as $k){
                    $readid=$k->id;
                    $readname=$k->nickname;
                    $backgroud=$k->backgroud;
                }
                 session(['readtel'=>$request->tel]);
                session(['readid'=>$readid]);
                session(['readname'=>$readname]);
                return redirect('home/index');
            }else{
               
                $request->session()->flash('qqerror', '1');
                return redirect('home/login');

            }

            
    }

public function https_request($url=0,$data=0)
{
        // curl 初始化
        $ch = curl_init();

        // 参数设置 
        curl_setopt($ch,CURLOPT_URL,$url);
        curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
        curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);

        // 这里进行 判断
        if ( !empty($data) ) {
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch,CURLOPT_POSTFIELDS,$data);

        }
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        // 执行
        $res = curl_exec($ch);
        // curl_close($ch);
        return $res;
        }


        function __autoload($classname)
        {
        if ( file_exists('./Controller/'.$classname.'.php') ) {
        include './Controller/'.$classname.'.php';
        }
}
}
